package com.jiamao.goplay.base;

import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.app.SkinAppCompatDelegateImpl;
import androidx.fragment.app.Fragment;

import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.ui.user.UserEditUserInfoFragment;
import com.kongzue.dialog.util.DialogSettings;
import com.umeng.analytics.MobclickAgent;

import me.goldze.mvvmhabit.base.ContainerActivity;
import skin.support.SkinCompatManager;
import skin.support.content.res.SkinCompatResources;

public class LibContainerActivity extends ContainerActivity {

    public int uiModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (TextUtils.isEmpty(SkinCompatResources.getInstance().getSkinPkgName())){
            uiModel = this.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
            if (uiModel == Configuration.UI_MODE_NIGHT_YES ){
                DialogSettings.theme = DialogSettings.THEME.DARK;
                SkinCompatManager.getInstance().loadSkin("night",  SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN); // 后缀加载
            }
             else {
                DialogSettings.theme = DialogSettings.THEME.LIGHT;
             SkinCompatManager.getInstance().restoreDefaultTheme();
             }
        }

        //AppManager.getAppManager().addActivity(this);
    }

    @Override
    public AppCompatDelegate getDelegate() {
        return SkinAppCompatDelegateImpl.get(this, this);
    }


    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);


        uiModel = this.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
        if (uiModel == Configuration.UI_MODE_NIGHT_YES){
            DialogSettings.theme = DialogSettings.THEME.DARK;
            ImmersionBar.with(this).statusBarDarkFont(false).init();
            SkinCompatManager.getInstance().loadSkin("night",  SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN); // 后缀加载
        }  else if (uiModel == Configuration.UI_MODE_NIGHT_NO ){
            DialogSettings.theme = DialogSettings.THEME.LIGHT;
            ImmersionBar.with(this).statusBarDarkFont(true).init();
            SkinCompatManager.getInstance().restoreDefaultTheme();
        }
    }

    // Fragment页面onResume函数重载
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this); //统计页面("MainScreen"为页面名称，可自定义)
    }
    // Fragment页面onResume函数重载
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }



    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (getSupportFragmentManager().getFragments().size()>=1){
            Fragment fragment = getSupportFragmentManager().getFragments().get(0);
            if (fragment instanceof UserEditUserInfoFragment){
               return  ((UserEditUserInfoFragment)fragment).onKeyDown(keyCode,event);
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
